<?php  
class ControllerModuleCategory extends Controller {
	protected $category_id = 0;
	protected $path = array();
	
	protected function index() {
		$this->language->load('module/category');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/category');
		$this->load->model('tool/seo_url');    		
		
		$this->data['template'] = $this->config->get('config_template');
        $this->data['giftcard_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=checkout/giftcard');

		//$this->data['category'] = $this->getCategories(0);		   
        $this->data['path'] = '';
        if (isset($this->request->get['path'])) {
            $this->path = explode('_', $this->request->get['path']);                
            $this->category_id = end($this->path);
            $this->data['path'] = $this->request->get['path'];    
        }        
        //$this->data['categories'] = $this->getAllCategories(0);
		$this->data['categories'] = $this->getAllCategories(0, $this->path);
		
		$this->data['template'] = $this->config->get('config_template');
		
		$this->id = 'category';

        if($this->request->get['route']=='common/home' || !$this->request->get['route']){
            $this->children[] = 'module/product_left';
        }

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
		} else {
			$this->template = 'default/template/module/category.tpl';
		}
		
		$this->render();
  	}
	
	protected function getCategories($parent_id, $current_path = '') {
		$category_id = array_shift($this->path);
		
		$output = '';
		
		$results = $this->model_catalog_category->getCategories($parent_id);
		
		if ($results) { 
			$output .= '<ul>';
    	}
		
		foreach ($results as $result) {	
			if (!$current_path) {
				$new_path = $result['category_id'];
			} else {
				$new_path = $current_path . '_' . $result['category_id'];
			}
			
			$output .= '<li>';
			
			$children = '';                                  
			
			if ($category_id == $result['category_id']) {
				$children = $this->getCategories($result['category_id'], $new_path);
			}
			
			if ($this->category_id == $result['category_id']) {
				$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '"><b>' . $result['name'] . '</b></a>';
			} else {
				$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '">' . $result['name'] . '</a>';
			}
			
        	$output .= $children;
        
        	$output .= '</li>'; 
		}
 
		if ($results) {
			$output .= '</ul>';
		}
		
		return $output;
	}	
	
	protected function getAllCategories($parent_id, $path = null) { 
		$results = $this->model_catalog_category->getCategories($parent_id);
		$data = array();
		
		if ($results) {            
			foreach ($results as $result) {	
				$children = $this->model_catalog_category->getCategories($result['category_id']);
				
				$children_data = array();
				if (in_array($result['category_id'], $path)) {					
					foreach ($children as $child) {
						$sub_children = array();
						if (in_array($child['category_id'], $path)) {
							$s_children = $this->model_catalog_category->getCategories($child['category_id']);							
							foreach ($s_children as $s_child) {
								$link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'] . '_' . $child['category_id'] . '_' . $s_child['category_id'];          
								
								$sub_children[] = array(
									'id' 		=> $s_child['category_id'], 
									'name'		=> $s_child['name'],
									'href'		=> $this->model_tool_seo_url->rewrite($link),
									'path'		=> $result['category_id'] . '_' . $child['category_id'] . '_' . $s_child['category_id'],
									'children' 	=> null,		
								);	
							}
						}						
						
						$link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'] . '_' . $child['category_id'];          
						$children_data[] = array(
							'id' 		=> $child['category_id'], 
							'name'		=> $child['name'],
							'href'		=> $this->model_tool_seo_url->rewrite($link),
							'path'		=> $result['category_id'] . '_' . $child['category_id'],
							'children' 	=> $sub_children,		
						);											
					}
				}							
				
                if($children || !$path){
                    //$link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $result['category_id'];
                    $link = HTTP_SERVER . 'index.php?route=product/manufacturer/category&amp;path=' . $result['category_id'];
                }
                else{
                    $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'];                        
                }             				                                                                         
                
				$data[] = array(
					'id' 		=> $result['category_id'], 
					'name'		=> $result['name'],
					'href'		=> $this->model_tool_seo_url->rewrite($link),
					'path'		=> $result['category_id'],
					'children' 	=> $children_data,					
				);							 				 
			}
    	}    		
		
		return $data;
	}	
	
	protected function getAllCategoriesOld($parent_id, $current_path = '') { 
		$results = $this->model_catalog_category->getCategories($parent_id);
		$data = array();
		
		if ($results) {            
			foreach ($results as $result) {	
				if (!$current_path) {
					$new_path = $result['category_id'];
				} 
				else{
					$new_path = $current_path . '_' . $result['category_id'];
				} 			
				                
				$children = $this->getAllCategories($result['category_id'], $new_path);
                
                if($children){
                    $link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $new_path;
                }			
                elseif(!isset($current_path) || ($current_path == '')){
                    //$link = HTTP_SERVER . 'index.php?route=product/manufacturer&amp;path=' . $new_path;
                    $link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $new_path;
                }
                else{
                    $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path;                        
                }             				                                                                         
                
				$data[] = array(
					'id' 		=> $result['category_id'], 
					'name'		=> $result['name'],
					'href'		=> $this->model_tool_seo_url->rewrite($link),
					'path'		=> $new_path,
					'children' 	=> $children,					
				);							 				 
			}
    	}    		
		
		return $data;
	}			
}
?>